package com.parking.status.sync;

import com.parking.status.sync.netty.server.TCPServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * @author shiyang Created on Jun 26, 2015 3:48:15 PM
 */
@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(Application.class, args);
//        Map<String, Object> yamlMap = ctx.getBean("yamlMap", Map.class);
//        Map<String, Object> tcp = (Map<String, Object>) yamlMap.get("tcp");

        Resource resource = new ClassPathResource("/application.yaml");
        Properties props = null;
        try {
            props = PropertiesLoaderUtils.loadProperties(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (props == null) {
            return;
        }
        int port = Integer.parseInt(String.valueOf(props.getProperty("tcp.port")));
        int bossthreadcount = Integer.parseInt(String.valueOf(props.getProperty("boss.thread.count")));
        int workerthreadcount = Integer.parseInt(String.valueOf(props.getProperty("worker.thread.count")));
        boolean keepalive = Boolean.parseBoolean(String.valueOf(props.getProperty("so.keepalive")));
        int backlog = Integer.parseInt(String.valueOf(props.getProperty("so.backlog")));
        TCPServer tcpServer = new TCPServer(ctx, port, bossthreadcount, workerthreadcount, keepalive, backlog);
        try {
            tcpServer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
